package com.zhibang.controller;

import com.zhibang.service.AlipayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.UUID;

@Controller
@RequestMapping("/alipay")
public class AlipayController {

    @Autowired
    private AlipayService alipayService;

    @GetMapping("/pay")
    @ResponseBody
    public String pay() {
        try {
            // 生成订单号
            String outTradeNo = UUID.randomUUID().toString().replace("-", "");
            // 支付金额
            String totalAmount = "0.01"; // 测试金额设为0.01元
            // 订单名称
            String subject = "测试订单";

            return alipayService.createOrder(outTradeNo, totalAmount, subject);
        } catch (Exception e) {
            e.printStackTrace();
            return "支付请求生成失败";
        }
    }

    @GetMapping("/return")
    public String returnUrl() {
        // 支付成功后的同步跳转页面
        return "success"; // 需要创建success.html页面
    }

    @RequestMapping("/notify")
    @ResponseBody
    public String notifyUrl() {
        // 支付宝异步通知处理
        return "success"; // 必须返回success表示处理成功
    }
}